﻿function ShowHideValidationSummary(isShowed) {
    if (isShowed == true) {
        $("#ValidationSummary").show();
    }
    else {
        $("#ValidationSummary").hide();
    }
}

String.format = function () {
    var s = arguments[0];
    for (var i = 0; i < arguments.length - 1; i++) {
        var reg = new RegExp("\\{" + i + "\\}", "gm");
        s = s.replace(reg, arguments[i + 1]);
    }

    return s;
}

function CreateDropdownTooltip() {
    $("select:not(.ui-pg-selbox)").each(function () {
        var dropdown = $(this);
        dropdown.find('option').each(function () {
            var option = $(this);
            option.attr('title', option.text());
        });
        if (dropdown.attr('multiple') != 'multiple') {
            dropdown.change(function () {
                dropdown.attr('title', dropdown.find('option :selected').text());
            });
        }
    });
}

function CreateDatePickerControl(selector) {

    if (selector.indexOf('#') == -1) {
        selector = "#" + selector;
    }

    $(selector).datepicker({
        dateFormat: 'dd-M-yy'
        , constrainInput: true
        , showOn: 'button'
        , buttonImageOnly: true
        //, buttonImage: '../Images/iconDatePicker.gif' 
		, buttonImage: dateIconURL
    });

}

function IsIntegerNumber(string) {
    var numericExpression = /^[0-9]+$/;

    if (string.match(numericExpression)) {
        return true;
    } else {
        return false;
    }
}

function NumberOnlyTextBox(txtName) {
    $('#' + txtName).keypress(function (e) {
        //if the letter is not digit then display error and don't type anything
        if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
            return false;
        }
        return true;
    });
}

function DecimalTextBox(txtName) {
    $('#' + txtName).keypress(function (e) {
        //if the letter is not digit then display error and don't type anything
        if (e.which != 8 && e.which != 0 && e.which != 46 && (e.which < 48 || e.which > 57)) {
            return false;
        }

        var txtValue = document.getElementById(txtName);

        if (e.which == 46 && txtValue.value == "") return false;

        var PosDecimal = txtValue.value.indexOf('.');
        if (e.which == 46 && PosDecimal >= 0) return false;

        return true;
    });
}

function RegisterTextAreaMaxLength() {
    // ignore these keys   
    var ignore = [8, 9, 13, 33, 34, 35, 36, 37, 38, 39, 40, 46];
    // use keypress instead of keydown as that's the only    
    // place keystrokes could be canceled in Opera   
    var eventName = 'keypress';
    // handle textareas with maxlength attribute   
    $('textarea[maxlength]')
    // this is where the magic happens     
    .live(eventName, function (event) {
        var self = $(this),
            maxlength = self.attr('maxlength'),
            code = $.data(this, 'keycode');
        // check if maxlength has a value.       
        // The value must be greater than 0       
        if (maxlength && maxlength > 0) {
            // continue with this keystroke if maxlength
            // not reached or one of the ignored keys were pressed.         
            return (self.val().length < maxlength || $.inArray(code, ignore) !== -1);
        }
    })
    // store keyCode from keydown event for later use     
    .live('keydown', function (event) {
        $.data(this, 'keycode', event.keyCode || event.which);
    });
};

/**
Combobox Customize
*/

function RegisterCombo(selector, width, fnSelectedCallback) {

    $(selector).customCombo({
        suffix: "custom",
        isDropdown: true,
        width: width,
        autoFill: false,
        emptyText: "",
        changeCallback: (typeof fnSelectedCallback == "function") ? fnSelectedCallback : null,
        triggerSelectedFirstLoad: true
    });

}

function RegisterComboWithEmptyText(selector, width, emptyText, fnSelectedCallback) {

    $(selector).customCombo({
        suffix: "custom",
        isFilter: true,
        isDropdown: false,
        width: width,
        autoFill: false,
        emptyText: emptyText,
        changeCallback: (typeof fnSelectedCallback == "function") ? fnSelectedCallback : null,
        triggerSelectedFirstLoad: false
    });

}

function RegisterComboFilter(selector, width, fnSelectedCallback) {

    $(selector).customCombo({
        suffix: "custom",
        isFilter: true,
        width: width,
        autoFill: true,
        emptyText: "",
        changeCallback: (typeof fnSelectedCallback == "function") ? fnSelectedCallback : null,
        triggerSelectedFirstLoad: true
    });
}

function DisableCombo(selector, isDisabled) {
    if (isDisabled) {
        $.customCombo.deactivate(selector);
    } else {
        $.customCombo.activate(selector);
    }
}

function ComboGetText(selector) {
    return $.customCombo.getText(selector);
}

function ComboGetValue(selector) {
    return $.customCombo.getValue(selector);
}

function ComboSetValue(selector, value) {
    return $.customCombo.setValue(selector, value);
}

function ComboReload(selector, isDefaultBlank) {
    return $.customCombo.changeOptions(selector, isDefaultBlank);
}

function ComboReloadByJsonData(selector, result, opts) {

    var keyValue = (typeof opts.KeyValue == 'undefined') ? 'Id' : opts.KeyValue;
    var keyText = (typeof opts.KeyText == 'undefined') ? 'Name' : opts.KeyText;
    var isDefaultBlank = (typeof opts.DefaultBlank == 'undefined') ? false : opts.DefaultBlank;

    $(selector).find('option').remove().end();
    if (result != null) {
        for (var i = 0; i < result.length; i++) {
            var item = result[i];
            $(selector).append($('<option></option>').val(eval('item.' + keyValue)).html(eval('item.' + keyText)));
        }
    }
    ComboReload(selector, isDefaultBlank);
}

function GetDataCombo(selector, opts) {

    var urlAction = (typeof opts.Url == 'undefined') ? '#' : opts.Url;
    var keyValue = (typeof opts.KeyValue == 'undefined') ? 'Id' : opts.KeyValue;
    var keyText = (typeof opts.KeyText == 'undefined') ? 'Name' : opts.KeyText;
    var jsonData = (typeof opts.PostData == 'undefined') ? '' : opts.PostData;
    var isDefaultBlank = (typeof opts.DefaultBlank == 'undefined') ? false : opts.DefaultBlank;

    $.ajax({
        type: "POST",
        url: urlAction,
        data: jsonData,
        dataType: "json",
        cache: false,
        success: function (result) {
            $(selector).find('option').remove().end();
            if (result != null) {
                for (var i = 0; i < result.length; i++) {
                    var item = result[i];
                    $(selector).append($('<option></option>').val(eval('item.' + keyValue)).html(eval('item.' + keyText)));
                }
            }
            ComboReload(selector, isDefaultBlank);
        },
        error: function (errorObj) {
            $(selector).find('option').remove().end();
        }
    });
}

/**
Accordion & Accordion Expand
*/
function ExpandAccordion(selector, isExpand) {
    $(selector).accordion("activate", (isExpand == true ? 0 : 1));
}

function RegisterAccordion(selector, isCollapsible, isActive, fnAccordionOnChange) {
    $(selector).accordion({
        collapsible: isCollapsible,
        autoHeight: true,
        heightStyle: "content",
        active: isActive,
        change: function (event, ui) {
            if (typeof fnAccordionOnChange == "function") {
                fnAccordionOnChange(event, ui);
            }
        }
    });
}

function RegisterAccordionCustom(selector, isCollapsed) {

    //Using css of jquery ui
    $(selector).addClass("ui-accordion ui-widget ui-helper-reset ui-accordion-icons");
    $(selector + " .accordion").addClass("ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active");

    if (isCollapsed) {
        $(selector + " h3").attr("class", "ui-accordion-header ui-helper-reset ui-state-default ui-corner-all");
        $(selector + " h3").append('<span class="ui-icon ui-icon-triangle-1-e"></span>');
        $(selector + " .accordion-collapsed").hide();
    } else {
        $(selector + " h3").attr("class", "ui-accordion-header ui-helper-reset ui-state-active ui-corner-top");
        $(selector + " h3").append('<span class="ui-icon ui-icon-triangle-1-s"></span>');
    }

    //Register accordion event for header of section
    $(selector + " h3").click(function () {
        if ($(selector + " .accordion-collapsed").css('display') == 'none') {
            $(selector + " h3").attr("class", "ui-accordion-header ui-helper-reset ui-state-active ui-corner-top");
            $(selector + " h3 span").attr('class', 'ui-icon ui-icon-triangle-1-s');
            $(selector + " .accordion-collapsed").show();
        } else {
            $(selector + " h3").attr("class", "ui-accordion-header ui-helper-reset ui-state-default ui-corner-all");
            $(selector + " h3 span").attr('class', 'ui-icon ui-icon-triangle-1-e');
            $(selector + " .accordion-collapsed").hide();
        }
    });
}

function EncodeHTML(strSource) {
    if (strSource != null) {
        return strSource.replace(/\&/g, '&amp;').replace(/\</g, '&lt;').replace(/\>/g, '&gt;').replace(/\"/g, '&quot;').replace(/\'/g, "&#39;");
    }
    else
        return strSource;
}

function DecodeHTML(strSource) {

    if (strSource != null) {
        return strSource.replace(/&amp;/gi, '&').replace(/&lt;/gi, '<').replace(/&gt;/gi, '>').replace(/&quot;/gi, '"').replace(/&#39;/gi, "'");
    }
    else
        return strSource;
}

function CallFunctionWithAjaxPost(opts, fnSuccessCallback) {
    $.ajax(
    	{
    	    type: "POST",
    	    url: opts.ActionUrl,
    	    data: opts,
    	    async: false,
    	    cache: false,
    	    dataType: "json",
    	    success: function (data) {
    	        fnSuccessCallback(data);
    	    }
    	});
}

/*  
Written by: Thuy Doan
Date: Nov 25, 2012
Description: set default item is the first item of dropdownlist
*/
function SetFirstItemIsDefaultItemDropdown(selector) {
    var value = selector.find('option:first').val();
    ComboSetValue(selector, value);
}

/* 
Written by: tdoan5 
Date: Nov 25, 2012
Clear text of all textbox control in a selector
*/
function ClearAllTextBoxControls(selector) {
    selector.find('input[type=text]').val('');
}

/* 
Written by: tdoan5 
Date: Nov 27, 2012
Only allow number only on textbox control (selector)
*/
function NumberOnly(selector) {
    selector.keypress(function (e) {
        //if the letter is not digit then display error and don't type anything
        if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
            return false;
        }
        return true;
    });
}

/* Hao Vo: 
Date updated: Jan 11, 2013
*/
function CallFunctionWithAjaxPostWithContentType(opts, fnSuccessCallback, fnCompleteCallback, fnErrorCallback) {
    $.ajax(
    	{
    	    type: "POST",
    	    url: opts.ActionUrl,
    	    data: JSON.stringify(opts.data),
    	    contentType: 'application/json',
    	    async: false,
    	    cache: false,
    	    dataType: "json",
    	    success: function (data) {
    	        if (typeof fnSuccessCallback == "function") {
    	            fnSuccessCallback(data);
    	        }
    	    },
    	    complete: function (data) {
    	        if (typeof fnCompleteCallback == "function") {
    	            fnCompleteCallback(data);
    	        }
    	    },
    	    error: function (data) {
    	        if (typeof fnErrorCallback == "function") {
    	            fnErrorCallback(data);
    	        }
    	    }
    	});
}